//
//  AppDelegate.swift
//  FallowProject
//
//  Created by 李晋 on 2017/7/25.
//  Copyright © 2017年 李晋. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        addNotificationForOrientation()
        return true
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        
    }

    func applicationWillTerminate(_ application: UIApplication) {
        
    }
}

// MARK: - 旋转屏幕
extension AppDelegate {
    
    func addNotificationForOrientation() -> Swift.Void {
        if !UIDevice.current.isGeneratingDeviceOrientationNotifications {
            UIDevice.current.beginGeneratingDeviceOrientationNotifications()
        }
        NotificationCenter.default.addObserver(self, selector: #selector(notiesDeviceCurrentorientation(notification:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(handleStatusBarOrientationChange(notification:)), name: NSNotification.Name.UIApplicationDidChangeStatusBarOrientation, object: nil)
    }
    
    @objc private func notiesDeviceCurrentorientation(notification: Notification) -> Swift.Void {
        let deviceOrientation = UIDevice.current.orientation
        switch deviceOrientation {
        case .faceUp:
            print("屏幕朝上平躺")
        case .faceDown:
            print("屏幕朝下平躺")
        case .landscapeLeft:
            print("屏幕向左横置")
        case .landscapeRight:
            print("屏幕向右横置")
        case .portrait:
            print("屏幕直立")
        case .portraitUpsideDown:
            print("屏幕直立，上下顛倒")
        default:
            print("Unknown")
        }
    }
    
    /// statusBar监听
    func handleStatusBarOrientationChange(notification: Notification) -> Swift.Void {
        let interfaceOrientation = UIApplication.shared.statusBarOrientation
        switch interfaceOrientation {
        case .unknown:
            print("未知方向")
        case .portrait:
            print("界面直立")
        case .portraitUpsideDown:
            print("界面直立，上下颠倒")
        case .landscapeLeft:
            print("界面朝左")
        case .landscapeRight:
            print("界面朝右")
        }
    }
}

